package com.wind.taobao.api.huabao.convert
{
	import com.wind.taobao.api.huabao.model.Poster;
	import com.wind.taobao.core.util.TaobaoConstants;

	import mx.collections.ArrayCollection;


	/**
	 * Poster转换器
	 * @author fxy
	 *
	 */
	public class PosterConvert
	{
		/**
		 * 转换Poster集合
		 */
		public static function convertPosters(value:Object, type:String="json"):ArrayCollection
		{
			var posters:ArrayCollection=new ArrayCollection();
			if (value != null)
			{
				if (type == TaobaoConstants.JSON)
				{
					if ((value as Array).length > 0)
					{
						for (var i:uint=0; i < (value as Array).length; i++)
						{
							posters.addItem(convertPoster((value as Array)[i]));
						}
					}
				}
				else
				{
					//TODO
				}
			}
			return posters;
		}

		/**
		 * 转换Poster
		 */
		public static function convertPoster(value:Object, type:String="json"):Poster
		{
			var poster:Poster=new Poster();
			if (value == null)
				return poster;
			if (type == TaobaoConstants.XML)
			{
				poster=convertXMLToPoster(XML(value), poster);
			}
			else
			{
				poster=convertJSONToPoster(value, poster);
			}
			return poster;
		}

		/**
		 * JSON转换为Poster
		 */
		private static function convertJSONToPoster(json:Object, poster:Poster):Poster
		{
			if (json.hasOwnProperty(TaobaoConstants.ID))
			{
				poster.id=json[TaobaoConstants.ID];
			}
			if (json.hasOwnProperty(TaobaoConstants.CREATED))
			{
				poster.created=json[TaobaoConstants.CREATED];
			}
			if (json.hasOwnProperty(TaobaoConstants.MODIFIED))
			{
				poster.modified=json[TaobaoConstants.MODIFIED];
			}
			if (json.hasOwnProperty(TaobaoConstants.TITLE))
			{
				poster.title=json[TaobaoConstants.TITLE];
			}
			if (json.hasOwnProperty(TaobaoConstants.SHORT_TITLE))
			{
				poster.short_title=json[TaobaoConstants.SHORT_TITLE];
			}

			if (json.hasOwnProperty(TaobaoConstants.TAGS))
			{
				poster.tags=json[TaobaoConstants.TAGS];
			}
			if (json.hasOwnProperty(TaobaoConstants.WEIGHT))
			{
				poster.weight=json[TaobaoConstants.WEIGHT];
			}
			if (json.hasOwnProperty(TaobaoConstants.COVER_URLS))
			{
				poster.cover_urls=json[TaobaoConstants.COVER_URLS];
			}
			if (json.hasOwnProperty(TaobaoConstants.HITS))
			{
				poster.hits=json[TaobaoConstants.HITS];
			}
			if (json.hasOwnProperty(TaobaoConstants.CHANNEL_ID))
			{
				poster.channel_id=json[TaobaoConstants.CHANNEL_ID];
			}
			return poster;
		}

		/**
		 * XML转换为Poster
		 */
		private static function convertXMLToPoster(value:XML, poster:Poster):Poster
		{
			return poster;
		}


	}
}